package com.vx.ratingTool.controllers;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.vx.ratingTool.entities.Player;
import com.vx.ratingTool.entities.SelectedPlayer;
import com.vx.ratingTool.services.IMatchService;
import com.vx.ratingTool.services.IPlayerBaseManager;

@Controller
public class Simulation {
	@Autowired
	private IPlayerBaseManager playerBaseManager;
	@Autowired
	private IMatchService matchService;
	@Autowired
	private SelectedPlayer selected_player;

	@RequestMapping(value = "/playMatchWithSelectPlayer", method = RequestMethod.GET)
	public String playMatchWithSelectedPlayerRequest(Model model) {

		if (selected_player.getId() == -1) {
			model.addAttribute("teamAwins", "No player selected");
		} else {

			matchService.findMatchForSelectedPlayer(playerBaseManager.getById(selected_player.getId()), 200);
			List<Player> foundList = new ArrayList<Player>(matchService.getFoundPlayers());
			model.addAttribute("foundPlayers", foundList);

			matchService.simulateMatch();
			matchService.saveResult();

			model.addAttribute("selected", "Selected player: " + playerBaseManager.getById(selected_player.getId()).getName());
			model.addAttribute("teamAplayers", matchService.getMatch().getTeamA());
			model.addAttribute("teamBplayers", matchService.getMatch().getTeamB());
			model.addAttribute("teamAskill", matchService.getMatch().getTeamAskill());
			model.addAttribute("teamBskill", matchService.getMatch().getTeamBskill());
			model.addAttribute("teamArating", matchService.getMatch().getTeamArating());
			model.addAttribute("teamBrating", matchService.getMatch().getTeamBrating());
			model.addAttribute("teamAchance", matchService.getMatch().getChanceToWinOfTeamAInPercent());
			model.addAttribute("teamAchanceRating", matchService.getMatch().getChanceToWinOfTeamABasedOnRatingPercent());
			if (matchService.getMatch().getTeamAwins())
				model.addAttribute("teamAwins", "Team A");
			else
				model.addAttribute("teamAwins", "Team B");
		}
		return "playMatchWithSelectPlayer";
	}

	@RequestMapping(value = "/simulateNMatches", method = RequestMethod.GET)
	public void setupForm() {
	}

	@RequestMapping(value = "/simulateNMatches", method = RequestMethod.POST)
	public String sumbitForm(@RequestParam("count") int count, Model model) {

		Random rnd = new Random();

		for (int i = 0; i < count; i++) {
			// finding random base player to search from his perspective
			Player selected = playerBaseManager.getById(rnd.nextInt(playerBaseManager.getListOfPlayers().size()));

			matchService.findMatchForSelectedPlayer(selected, 200);

			matchService.simulateMatch();

			matchService.saveResult();

		}

		String success = "Success";
		model.addAttribute("success", success);
		return "simulateNMatches";
	}

	@RequestMapping(value = "/simulateSingleRandomMatch", method = RequestMethod.GET)
	public String createPlayers(Model model) {
		Random rnd = new Random();

		Player selected = playerBaseManager.getById(rnd.nextInt(playerBaseManager.getListOfPlayers().size()));

		matchService.findMatchForSelectedPlayer(selected, 200);
		List<Player> foundList = new ArrayList<Player>(matchService.getFoundPlayers());
		model.addAttribute("foundPlayers", foundList);

		matchService.simulateMatch();
		matchService.saveResult();

		model.addAttribute("teamAplayers", matchService.getMatch().getTeamA());
		model.addAttribute("teamBplayers", matchService.getMatch().getTeamB());
		model.addAttribute("teamAskill", matchService.getMatch().getTeamAskill());
		model.addAttribute("teamBskill", matchService.getMatch().getTeamBskill());
		model.addAttribute("teamArating", matchService.getMatch().getTeamArating());
		model.addAttribute("teamBrating", matchService.getMatch().getTeamBrating());
		model.addAttribute("teamAchance", matchService.getMatch().getChanceToWinOfTeamAInPercent());
		model.addAttribute("teamAchanceRating", matchService.getMatch().getChanceToWinOfTeamABasedOnRatingPercent());
		if (matchService.getMatch().getTeamAwins())
			model.addAttribute("teamAwins", "Team A");
		else
			model.addAttribute("teamAwins", "Team B");

		return "simulateSingleRandomMatch";
	}

}
